package vsm.vsm_server.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import vsm.vsm_server.pojo.Comment.*;

import java.math.BigInteger;
import java.util.List;

@Mapper
public interface CommentMapper {

    @Insert("insert into comments (userId,videoId,content,createTime,rootCommentId,toCommentId,getterId,getterName)values (#{userId},#{videoId},#{content},#{createTime},#{rootCommentId},#{toCommentId},#{getterId},#{getterName})")
    @Options(useGeneratedKeys = true,keyProperty = "commentId",keyColumn = "commentId")
    Boolean sendComment(Comment comment);


    @Select("select * , (SELECT COUNT(*) FROM comments sub_c WHERE sub_c.rootCommentId = c.commentId AND sub_c.isDelete = 0) AS childCommentCount from comments c where videoId = #{videoId} and rootCommentId is null and isDelete = false order by createTime desc")
    List<CommentRoot> getRootCommentsByVideoId(RootCommentGetDto rootCommentGetDto);

    @Select("select *  from comments where rootCommentId = #{rootCommentId} and isDelete = false order by createTime desc")
    List<CommentChild> getChildCommentsByRootId(ChildCommentGetDto childCommentGetDto);

    @Select("select count(*) from comments where videoId = #{videoId} and isDelete = false")
    Double getCommentCount(BigInteger videoId);

    @Select("select userId from comments where commentId = #{commentId} and isDelete = false")
    BigInteger getCommentSenderId(BigInteger commentId);
}
